module.exports = function ($scope, $resource, $modal, toaster, $state){

    $scope.searchform = {};

    /* 分页
       * ========================================= */
    $scope.maxSize = 5;            //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条

    $scope.load = function () {
        var para = {
            pageNo: $scope.bigCurrentPage,
            pageSize: $scope.itemsPerPage
        };

        angular.extend(para, $scope.searchform);

        $resource('/api/as/goc/infodiffusion/problemlist', {}, {}).save(para, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.objs = res.data.results;
            $scope.bigTotalItems = res.data.totalRecord;

        });
    };
    $scope.load();

    //创建
    $scope.create = function(obj){
        var para = $state.get('app.createproblem');
        var resolve = {
            obj : function(){
                return obj;
            },
            what : function(){
                return 'create';
            }
        };
        angular.extend(para.resolve, resolve);

        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
            console.log('modal is opened');  
        });  
        modalInstance.result.then(function(result) {  
            $scope.load();
        }, function(reason) {  
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    }

    //编辑
    $scope.edit = function(obj){
        var para = $state.get('app.createproblem');
        var resolve = {
            obj : function(){
                return obj;
            },
            what : function(){
                return 'edit';
            }
        };
        angular.extend(para.resolve, resolve);

        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
            console.log('modal is opened');  
        });  
        modalInstance.result.then(function(result) {  
            $scope.load();
        }, function(reason) {  
            console.log(reason);// 点击空白区域，总会输出backdrop  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 
    }

    

    //提交
	$scope.commit = function(id) {
		$resource('/api/as/goc/infodiffusion/updateproblemstate', {}, {}).save({'id' : id}, function(res){
            if (res.errcode === 0) {
                toaster.success({title: '', body: '操作成功'});
                $scope.load();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

};